﻿//#define TRANSFER_PROTOTYPE

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Threading;

using agree.composition;
using agree.schema;
using agree.mrs;

using alib;
using alib.Collections;
using alib.Debugging;
using alib.Enumerable;
using alib.String;
using alib.String.Builder;
using alib.Hashing;
using alib.Matrix;

namespace agree
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public sealed class MrsToTfsBag : m_scalar_base<Mrs, TfsBag>, ρρGrammar
	{
		public MrsToTfsBag(IRtParent parent)
			: base(parent)
		{
		}
		public MrsToTfsBag(IRtParent parent, Mrs mrs)
			: base(parent, mrs)
		{
		}

		///////////////////////////////////////////////////////////////////////
		///
		Grammar g;
		public Grammar Grammar
		{
			get { return g ?? (g = (Grammar)P.Grammar.FindTracePropertyValue(t0)); }
			set { this.g = value; }
		}
		///
		///////////////////////////////////////////////////////////////////////

		protected override TfsBag start(Mrs arg)
		{
			return TfsBag.FromXmrs(this, arg, this.Grammar);
		}
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public sealed class PassthroughPairTransfer : m_scalar_base<IIdentPair<Mrs>, IIdentPair<Mrs>>
	{
		const String f_proto =
#if TRANSFER_PROTOTYPE
 "prototype";
#else
 "full";
#endif

		public PassthroughPairTransfer(IRtParent parent)
			: base(parent)
		{
		}
		public PassthroughPairTransfer(IRtParent parent, IIdentPair<Mrs> pr)
			: base(parent, pr)
		{
		}

		protected override IIdentPair<Mrs> start(IIdentPair<Mrs> pr)
		{
#if false
			transfer_demo(pr.x, pr.y);
#endif
			return pr;
		}

		void transfer_demo(Mrs mrs_x, Mrs mrs_y)
		{
			var i_x = (StringItem)P.SourceItem.FindTracePropertyValue(mrs_x);
			var i_y = (StringItem)P.SourceItem.FindTracePropertyValue(mrs_y);
			var g_x = (Grammar)P.Grammar.FindTracePropertyValue(mrs_x);
			var g_y = (Grammar)P.Grammar.FindTracePropertyValue(mrs_y);


			StringBuilder sb = new StringBuilder();
			sb.AppendFormatLine("============ start transfer ({0}) ============", f_proto);
			sb.AppendFormatLine("source: [{0}] {1}\r\n{2}", g_x.Name, i_x.Text.DoubleQuote(), mrs_x.ToString().Indent(4));
			sb.AppendFormatLine("expect: [{0}] {1}\r\n{2}", g_y.Name, i_y.Text.DoubleQuote(), mrs_y.ToString().Indent(4));
			Debug.WriteLine(sb.ToString());

#if TRANSFER_PROTOTYPE
			var tp = new transfer_prototype.TransferExecutor(this, mrs_x);
			var xfer_result = (IIdentList<transfer_prototype.xfer_hyp>)tp.ProvideValue(null);
#else
			var tp = new transfer.TransferExecutor(this, mrs_x);
			var xfer_result = (IIdentList<transfer.Hypothesis>)tp.ProvideValue(null);
#endif

			Debug.Print("{0} transfer results", xfer_result.Count);
		}
	};
}
